package com.goodving.guli.activity;

import android.app.ActivityManager;
import android.app.NotificationManager;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;

import com.goodving.guli.R;
import com.goodving.guli.bt.BLEService;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.C0708b;
import com.goodving.guli.bt.C0715c;
import com.goodving.guli.bt.C1041c;
import com.goodving.guli.bt.C3917o;
import com.goodving.guli.bt.SyncWeightDataCB;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.dataprocess.SportDay;
import com.goodving.guli.bt.eventbus.EventBtOnOff;
import com.goodving.guli.bt.eventbus.EventDeviceBoundState;
import com.goodving.guli.bt.eventbus.EventScaleStatus;
import com.goodving.guli.bt.eventbus.EventWeightSyncedDevice;
import com.goodving.guli.bt.eventbus.EventWifiConnected;
import com.goodving.guli.bt.lua.LuaEvent;
import com.goodving.guli.bt.model.Birthday;
import com.goodving.guli.bt.model.C1495q;
import com.goodving.guli.bt.model.C1981P;
import com.goodving.guli.bt.model.DataManager;
import com.goodving.guli.bt.model.HwBatteryStatus;
import com.goodving.guli.bt.model.HwSyncDataStatus;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.model.WeightInfo;
import com.goodving.guli.bt.model.WeightInfoManager;
import com.goodving.guli.bt.profile.scale.C1798h;
import com.goodving.guli.bt.profile.scale.C3843c;
import com.goodving.guli.bt.profile.scale.C3900g;
import com.goodving.guli.bt.profile.scale.C3907e;
import com.goodving.guli.bt.profile.scale.C3923a;
import com.goodving.guli.bt.profile.scale.C3942j;
import com.goodving.guli.bt.profile.scale.FilterBleDevInfo;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.HwConnStatus;
import com.goodving.guli.bt.profile.scale.ScaleWeightInfo;
import com.goodving.guli.bt.profile.scale.WeightProfile;
import com.goodving.guli.bt.profile.scale.XMBLEManager;
import com.goodving.guli.bt.profile.scale.XMLeScanCenter;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.bt.weight.LuaUtil;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.model.C1222e;
import com.goodving.guli.model.DeviceSource;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.server.ClientDataConstants;
import com.goodving.guli.server.C1327j;
import com.goodving.guli.server.WebAPI;
import com.goodving.guli.server.WebResponse;
import com.goodving.guli.utility.C1481u;
import com.goodving.guli.view.C3247b;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4220a;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import cn.com.smartdevices.bracelet.config.C0886a;
import cz.msebera.android.httpclient.Header;
import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/11/30.
 */
public class BaseSCActivity extends SystemBarTintActivity implements C1798h {
    private static final int f8793c = 8193;
    private static final int f8794d = 8194;
    private static final int f8795e = 8195;
    private static final int f8796f = 30000;
    private static final int f8797g = 4098;
    private static final int f8798h = 4099;
    private static final int f8799i = 4100;
    private static final int f8800j = 4101;
    private static final int f8801k = 4102;
    private static final int f8802l = 4103;
    private static final int f8803m = 4104;
    private static final int f8804n = 4105;
    private static final int addWeightConnectFailedDynamic = 4112;
    private static final int f8806p = 4113;
    private static final int f8807q = 4114;
    private static final int f8808r = 4115;
    private static final int f8809s = 4116;
    private static final int f8810t = 39;
    private static final String TAG = "BaseSCActivity";
    private boolean f8812A;
    private boolean f8813B;
    private boolean f8814C;
    private boolean f8815D;
    private XMLeScanCenter bleScanCenter;
    private FilterBleDevInfo mFilterBleDevInfo;
    private boolean f8818G;
    private boolean f8819H;
    private long f8820I;
    private long f8821J;
    private boolean f8822K;
    private boolean f8823L;
    private final int f8824M;
    private final int f8825N;
//    protected C1693p f8826a;
//    private FwUpgradeManager f8827b;
    private Handler mHandler;
    private BroadcastReceiver mBroadcastReceiver;
    private Context mContext;
    private boolean f8831y;
    private boolean f8832z;

    public BaseSCActivity() {
        this.mHandler = null;
        this.mBroadcastReceiver = null;
        this.mContext = null;
        this.f8831y = false;
        this.f8832z = false;
        this.f8812A = false;
        this.f8813B = false;
        this.f8814C = false;
        this.f8815D = true;
//        this.f8826a = null;
        this.bleScanCenter = XMLeScanCenter.getInstance();
        this.mFilterBleDevInfo = null;
        this.f8818G = false;
        this.f8819H = false;
        this.f8820I = 0;
        this.f8821J = 0;
        this.f8823L = false;
        this.f8824M = 1;
        this.f8825N = 3;
    }

    class C1864y extends AsyncHttpResponseHandler {
        final /* synthetic */ BaseSCActivity f10425a;

        C1864y(BaseSCActivity baseSCActivity) {
            this.f10425a = baseSCActivity;
        }

        public void onFailure(int i, Header[] headerArr, byte[] bArr, Throwable th) {
            Keeper.setSyncWeightInfoToServer(1);
        }

        public void onSuccess(int i, Header[] headerArr, byte[] bArr) {
            if (bArr != null) {
                String str = new String(bArr);
                GuliLog.tracePrintInfo("BaseSCActivity", "updateBindedWeightInfo result:" + str);
                if (WebResponse.getWebResponse(this.f10425a.mContext, str).m11659c()) {
                    Keeper.setSyncWeightInfoToServer(2);
                }
            }
        }
    }

    private void updateBindedWeightInfo() { // m13596A
        C1327j.m11576a(getApplicationContext(), WebAPI.m11583a(getApplicationContext(), 1), new C1864y(this), true);
    }

    class C1865z extends AsyncHttpResponseHandler {
        final /* synthetic */ BaseSCActivity f10426a;

        C1865z(BaseSCActivity baseSCActivity) {
            this.f10426a = baseSCActivity;
        }

        public void onFailure(int i, Header[] headerArr, byte[] bArr, Throwable th) {
            Keeper.setSyncBraceletInfoToServer(1);
        }

        public void onSuccess(int i, Header[] headerArr, byte[] bArr) {
            if (bArr != null) {
                if (WebResponse.getWebResponse(this.f10426a.mContext, new String(bArr)).m11659c()) {
                    Keeper.setSyncBraceletInfoToServer(2);
                }
            }
        }
    }

    private void updateBindedBraceletInfo() { // m13597B
        C1327j.m11576a(getApplicationContext(), WebAPI.m11583a(getApplicationContext(), 0), new C1865z(this), true);
    }

    private void m13598C() {
        if (Keeper.getSyncWeightInfoToServer() == 1) {
            updateBindedWeightInfo();
        }
        if (Keeper.getSyncBraceletInfoToServer() == 1) {
            updateBindedBraceletInfo();
        }
        if (Keeper.getUploadImeiStatus() == 0) {
            GuliLog.m12370d(TAG, " last uploadImei failed, retry uploadIMEI()");
            Utils.m8104y(getApplicationContext());
        }
    }


    //TBD this is for ring
//    private void m13600a(int i, int i2) {
//        new Thread(new C1863x(this, i, i2)).start();
//    }

    private void m13603a(WeightInfo weightInfo) {
        WeightInfoManager a = WeightInfoManager.getInstance();
        boolean weightMergeResult = Keeper.readPersonInfo().getMiliConfig().getWeightMergeResult();
        WeightInfo e = a.m15777e(weightInfo.uid);
        if (weightInfo.uid != 0 && weightMergeResult && e != null && weightInfo.timestamp - e.timestamp <= 30000) {
            GuliLog.m12370d(TAG, "<<merge>> realtime weighting:<" + e + "><" + weightInfo + ">");
            WeightUtil.deleteWeightInfoMarkFully(e);
        }

        // store the weight into the local database.
        a.m15763b(weightInfo);
//        if (Config.m8876h().f4193c.f4248d.booleanValue() && weightInfo.uid == -1) {
//            QQHealth.getInstance(this.mContext.getApplicationContext()).addNeedSyncWeight(Long.valueOf(weightInfo.timestamp));
//        }
        EventBus.getDefault().post(new EventWeightSyncedDevice(weightInfo.uid));
    }

    private void m13604a(C3843c c3843c) {
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        boolean a = m13655a(this.mContext);
        GuliLog.m12370d(TAG, "isEnterInBackground:" + a + ",isScreenOn:" + powerManager.isScreenOn());
        if (!a && powerManager.isScreenOn()) {
            if (c3843c.m25104c()) {
                HashMap hashMap = new HashMap();
                hashMap.put(C0715c.f3783y, XMBLEManager.m25160e().f19707a + C3657a.f18328f);
                hashMap.put(C0715c.f3784z, XMBLEManager.m25160e().f19708b.toString() + C3657a.f18328f);
                C0668a.m8131a(this.mContext, C0708b.af, hashMap);
                m13642r();
            } else if (C3843c.SHOES.equals(c3843c)) {
                m13656a(false);
            }
        }
    }

    private void m13605a(HwConnStatus hwConnStatus) {
        GuliLog.m12370d(TAG, "HwConnStatus:" + hwConnStatus + ",type = " + hwConnStatus.m25399b());
        if (DeviceSource.hasBindShoesDevice() || DeviceSource.hasBindBracelet()) {
            EventBus.getDefault().postSticky(hwConnStatus);
            if (hwConnStatus.m25408k()) {
                m13604a(hwConnStatus.m25399b());
            }
        }
    }

    private void m13606a(ScaleWeightInfo c3925e) {
        C1981P c1981p = new C1981P();
        //TBD hack here, to change the weightino timestamp to current system time
        // now we can remove this  hack, we already get the timestamp when we can get the information
//        {
//            Calendar instance3 = Calendar.getInstance();
//            c3925e.setDate(instance3.getTimeInMillis());
//        }
        c1981p.weightInfo = c3925e;
        // TBD debug
//        Log.e(TAG, GuliLog.stackTracePrint() + c3925e.toString());

        if (!c3925e.isStable()) {
            c1981p.f11134b = getResources().getString(R.string.weight_weighting_now);
            c1981p.f11135c = 0;
            c1981p.f11136d = 0.0f;
        } else if (c3925e.isFinish()) {
            GuliLog.tracePrintInfo(TAG, "the data is finish not save!!!");
        } else {
            UserInfo userInfo = new UserInfo();
            userInfo.uid = 0;
            userInfo.name = getString(R.string.visitor);
            List a = WeightUtil.m15701a(c3925e);
            if (a != null && a.size() == 1) {
                userInfo = (UserInfo) a.get(0);
            }
            c1981p.userInfoList = a;
            if (userInfo.uid != 0) {
                c3925e.m25482a(WeightUtil.m15693a(userInfo.height, Birthday.fromStr(userInfo.birthday).getAge(), c3925e.m25500k()));
                c1981p.f11134b = userInfo.name;
                c1981p.f11135c = userInfo.uid;
                c1981p.f11136d = userInfo.targetWeight;
            } else {
                c1981p.f11134b = getResources().getString(R.string.weight_guest);
                c1981p.f11135c = 0;
                c1981p.f11136d = 0.0f;
            }
            Calendar instance = Calendar.getInstance();
            instance.clear(Calendar.MILLISECOND);
            Calendar instance2 = Calendar.getInstance();
            instance2.setTimeInMillis(c3925e.m25494e());
            if (instance.get(Calendar.YEAR) != instance2.get(Calendar.YEAR)) {
                GuliLog.m12370d(TAG, "update weight data timestamp!!!");
                c3925e.setDate(instance.getTimeInMillis());
            }
            // to check do we have this information in the data base.
            if (WeightInfoManager.getInstance().m15760b(c3925e.m25494e()) == null) {
                WeightInfo weightInfo = new WeightInfo(c3925e.m25500k(), userInfo.uid, c3925e.m25494e());
                weightInfo.deviceid = c3925e.m25481a();
                // when the result is stable, store it in the db.
                 m13603a(weightInfo);
                // to add the weightinfo to the lualist
                LuaUtil.getInstance(this.mContext).m15966b(userInfo, c3925e);
                this.mHandler.sendEmptyMessage(f8802l);
            } else {
                GuliLog.m12370d(TAG, "the data exist in database!!!");
            }
        }
        // to nofity DynamicFragment class to update the information on the screen.
        EventBus.getDefault().post(c1981p);
    }

    private boolean m13608a(boolean z, boolean z2) {
        boolean z3 = true;
//        boolean z4 = (z2 || DataManager.m12179a().m12239r() == C3843c.SHOES.m25103b()) ? false : true;
//        if (z4) {
//            return false;
//        }
//        if (this.f8826a == null) {
//            this.f8826a = new C1693p(this.mContext);
//        }
//        if (z) {
//            z3 = false;
//        } else if (System.currentTimeMillis() - Keeper.readSyncDeviceTime(C3843c.SHOES.m25103b()) > 120000) {
//            z3 = false;
//        }
        return z3;
    }

    class C1857w extends AsyncHttpResponseHandler {
        final /* synthetic */ long f10369a;
        final /* synthetic */ BaseSCActivity f10370b;

        C1857w(BaseSCActivity baseSCActivity, long j) {
            this.f10370b = baseSCActivity;
            this.f10369a = j;
        }

        public void onFailure(int i, Header[] headerArr, byte[] bArr, Throwable th) {
        }

        public void onSuccess(int i, Header[] headerArr, byte[] bArr) {
            String str = "yyyy-MM-dd HH:mm:ss";
            try {
                JSONObject jSONObject = new JSONObject(Utils.m8035b(bArr));
                if (WebResponse.getWebResponse(this.f10370b.mContext, jSONObject).m11659c()) {
                    jSONObject = jSONObject.getJSONObject("data");
                    String trim = jSONObject.getString("timedate").trim();
                    str = jSONObject.getString(ClientDataConstants.f6739j);
                    String id = TimeZone.getDefault().getID();
                    if (!str.equalsIgnoreCase(id)) {
                        trim = Utils.m7988a(trim, "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss");
                        if (trim == null) {
                            return;
                        }
                    }
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                    Date parse = simpleDateFormat.parse(trim);
                    Date date = new Date();
                    GuliLog.tracePrintInfo("BaseSCActivity", "ntp tz:" + str + ",local tz:" + id);
                    GuliLog.tracePrintInfo("BaseSCActivity", "ntp ts:" + trim + ",local ts:" + simpleDateFormat.format(date));
                    long time = parse.getTime() + ((SystemClock.elapsedRealtime() - this.f10369a) / 2);
                    if (Math.abs(time - date.getTime()) / 60000 > 0) {
                        GuliLog.m12370d("BaseSCActivity", "\n=========================ERROR=========================\nserver time:" + parse.toString() + "\n" + "local time :" + date.toString() + "\n" + "=========================ERROR=========================");
                        return;
                    }
                    Calendar instance = Calendar.getInstance();
                    instance.setTimeInMillis(time);
                    GuliLog.m12370d("BaseSCActivity", "deleteDataAfterDate:" + DataManager.m12179a().m12197a(new SportDay(instance).getKey(), new C1495q()));
                }
            } catch (Exception e) {
            }
        }
    }

    private void m13609b(Context context) {
        WebAPI.m11639f(C0886a.m9071f(context), new C1857w(this, SystemClock.elapsedRealtime()));
        return;
    }

//    private void m13612c(int i) {
//        GuliLog.m12360b(TAG, "onBatteryLowNotification:" + i);
//        int readLowBatteryNofifyLevel = Keeper.readLowBatteryNofifyLevel();
//        int a = HwBatteryStatus.m25395a(i);
//        if (readLowBatteryNofifyLevel != a) {
//            Keeper.keepLowBatteryNofityLevel(a);
//            NotificationManager notificationManager = (NotificationManager) getSystemService("notification");
//            CharSequence string = getString(R.string.noti_bracelet_low_battery_title);
//            CharSequence charSequence = C3657a.f18328f;
//            if (a == 0) {
//                charSequence = getString(R.string.noti_bracelet_low_battery_content_high);
//            } else if (a == 1 || a == 2) {
//                charSequence = getString(R.string.noti_bracelet_low_battery_content_mid);
//            } else if (a == 3) {
//                string = getString(R.string.noti_bracelet_low_battery_title_low);
//                charSequence = getString(R.string.noti_bracelet_low_battery_content_low);
//            }
//            aT e = new aT(this).m647a((int) R.drawable.app_icon).m664a(string).m673b(charSequence).m687e(true);
//            e.m668a(new long[]{100, 500, 500, 500});
//            e.m654a(PendingIntent.getBroadcast(this, 0, new Intent(C4220a.bM), 268435456));
//            notificationManager.notify(1, e.m676c());
//        }
//    }
//
//    private boolean m13616d(int i) {
//        String string;
//        PendingIntent pendingIntent;
//        GuliLog.tracePrintInfo(TAG, "notifyBraceletVibrated:" + i);
//        String str = C3657a.f18328f;
//        PendingIntent activity = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 268435456);
//        switch (i) {
//            case C4005t.WeightView_wv_tips /*14*/:
//                string = getString(R.string.notify_status_motor_call);
//                pendingIntent = activity;
//                break;
//            case C4220a.aV /*15*/:
//                string = getString(R.string.notify_status_motor_disconnect);
//                pendingIntent = activity;
//                break;
//            case C4220a.bp /*16*/:
//                string = getString(R.string.notify_status_motor_smart_alarm);
//                pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(C4220a.bN), 268435456);
//                break;
//            case C4220a.bu /*17*/:
//                string = getString(R.string.notify_status_motor_alarm);
//                pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(C4220a.bN), 268435456);
//                break;
//            case C4220a.br /*18*/:
//                string = getString(R.string.notify_status_motor_goal);
//                pendingIntent = activity;
//                break;
//            case C4220a.bs /*19*/:
//                string = getString(R.string.notify_status_motor_auth);
//                pendingIntent = activity;
//                break;
//            case C4220a.aF /*20*/:
//                string = getString(R.string.notify_status_motor_shut_down);
//                pendingIntent = activity;
//                break;
//            case C3934a.f19852Z /*21*/:
//                string = getString(R.string.notify_status_motor_auth_success);
//                pendingIntent = activity;
//                break;
//            case C3934a.aa /*22*/:
//                string = getString(R.string.notify_status_motor_test);
//                pendingIntent = activity;
//                break;
//            case C3934a.ab /*27*/:
//                return true;
//            default:
//                return false;
//        }
//        NotificationManager notificationManager = (NotificationManager) getSystemService("notification");
//        aT e = new aT(this).m647a((int) R.drawable.app_icon).m664a(getString(R.string.notify_status_motor) + string).m687e(true);
//        e.m654a(pendingIntent);
//        notificationManager.notify(f8810t, e.m676c());
//        return true;
//    }

    private boolean m13617d(boolean z) {
        boolean z2 = this.f8831y || this.f8832z;
//        m13631j();
        if (!DeviceSource.hasBindBracelet()) {
            return false;
        }
        DeviceSource.unbindSensorHub();
        if (z && XMBLEManager.m25158c()) {
            if (z2) {
                m13642r();
            } else if (this.f8818G) {
                this.f8818G = false;
                m13625g(true);
            }
        }
//        m13628i();
        return true;
    }

//    private void m13618e(int i) {
//        GuliLog.tracePrintInfo(TAG, "showFwUpgradeUI:" + this.f8813B);
//        if (!this.f8813B) {
//            this.f8813B = true;
//            Intent intent = new Intent();
//            intent.setClass(this, FwUpgradeActivity.class);
//            intent.putExtra(SettingContainerActivity.f9257a, i);
//            startActivityForResult(intent, i == 0 ? f8794d : f8795e);
//        }
//    }

    private boolean m13620e(boolean z) {
        if (DeviceSource.hasBindWeight()) {
            if (z) {
                if (m13635l()) {
                    this.mHandler.sendEmptyMessage(f8804n);
                } else {
                    m13636m();
                }
            }
            return true;
        }
        m13638n();
        return false;
    }

    private void m13622f(int i) {
        GuliLog.m12370d(TAG, "weight connect status:" + i);
        if (i == 1 || i == 0 || i == 4) {
            m13636m();
        } else if (i != 2 && i == 3) {
            this.f8823L = true;
            if (DeviceSource.hasBindWeight()) {
                this.mHandler.sendEmptyMessage(f8804n);
            }
            m13638n();
            GuliLog.m12370d(TAG, "removeWeightConnectFailedDynamic");
            this.mHandler.removeMessages(addWeightConnectFailedDynamic);
            if (LuaUtil.getInstance(this.mContext).m15967b()) {
                this.mHandler.sendEmptyMessage(f8802l);
            }
        }
    }

    private boolean m13623f(boolean z) {
        if (!DeviceSource.hasBindShoesDevice()) {
            return false;
        }
        DeviceSource.unbindSensorHub();
        if (z) {
            m13656a(this.f8819H);
            this.f8819H = false;
        }
        return true;
    }

    private void m13625g(boolean z) {
        XMBLEManager.m25165j();
//        if (z || this.f8827b.checkMiliFwUpgrade() || System.currentTimeMillis() - Keeper.readSyncTime(0) > 120000) {
//            new C3839z(new C0639F(this.mContext)).m9824f();
//        }
    }

    private void m13626h() {
        boolean c = m13662c(false);
        //TBD  here read from the config file to enable the weight usage, we cancell it for now
//        if (Config.m8876h().f4202l.f4259a.booleanValue()) {
            m13620e(c);
//        }
//        if (m13617d(c)) {
//            this.f8814C = true;
//        } else if (m13623f(c)) {
//            this.f8814C = true;
//        } else if (m13633k()) {
//            this.f8814C = true;
//        } else if (this.f8814C) {
//            m13661c();
//            this.f8814C = false;
//        }
    }

//    private void m13628i() {
//        C0634B a = C0634B.m7841a();
//        if (a.m7862f()) {
//            PersonInfo readPersonInfo = Keeper.readPersonInfo();
//            String str = Keeper.readBraceletBtInfo().f19636b;
//            if (BluetoothAdapter.checkBluetoothAddress(str) && readPersonInfo.isInComingCallEnabled() && !a.m7856c(Keeper.readBraceletBtInfo().f19636b)) {
//                GuliLog.m12370d(TAG, "checkMiuiIncallDelay setAlertIncall:" + readPersonInfo.getInComingCallTime());
//                a.m7859d(str, true);
//                a.m7850a(str, readPersonInfo.getInComingCallTime());
//                readPersonInfo.disableInComingCallTime();
//                readPersonInfo.setInComingCallTime(2);
//                readPersonInfo.setNeedSyncServer(2);
//                Keeper.keepPersonInfo(readPersonInfo);
//                Utils.m7998a(this.mContext, Keeper.readPersonInfo());
//            }
//        }
//    }

//    private boolean m13631j() {
//        String str = Keeper.readBraceletBtInfo().f19636b;
//        List<String> c = C0634B.m7841a().m7855c();
//        if (c != null) {
//            for (String str2 : c) {
//                GuliLog.m12370d(TAG, "miui service bound device:" + str2);
//                if (!str2.equalsIgnoreCase(str)) {
//                    C0634B.m7841a().m7849a(str2);
//                }
//            }
//        }
//        return true;
//    }

//    private boolean m13633k() {
//        boolean z = this.f8831y || this.f8832z;
//        if (!Utils.m8088m(this)) {
//            return false;
//        }
//        if (!DeviceSource.hasBindSensorHub()) {
//            m13639o();
//        }
//        onSensorHub();
//        if (z) {
//            C1218a.m11319a().m11336a(this.mContext, new C0639F(this.mContext, true));
//        }
//        return true;
//    }

    private boolean m13635l() {
        WeightProfile i = XMBLEManager.m25164i();
        return i != null && i.m25529u();
    }

    private void m13636m() {
        if (this.mFilterBleDevInfo == null) {
            this.mFilterBleDevInfo = new C3900g().setUUID(C3923a.y_).m25388a((C1798h) this).setTimeout(-1).create();
        }
        if (DeviceSource.hasBindWeight()) {
            if (((!m13655a(C1481u.getContext()) ? 1 : 0) != 0) | (this.f8815D != false)) {
                GuliLog.m12370d(TAG, "start scan for weight");
                this.f8815D = false;
                this.bleScanCenter.startScan(C1481u.getContext(), this.mFilterBleDevInfo);
                this.mHandler.sendEmptyMessageDelayed(4113, 30000);
            }
        }
    }


    private void m13638n() {
        GuliLog.m12370d(TAG, "stop scan for weight");
        if (this.mFilterBleDevInfo != null) {
            this.bleScanCenter.m25373a(this.mFilterBleDevInfo);
            this.mFilterBleDevInfo = null;
        }
        this.mHandler.sendEmptyMessage(f8807q);
    }

    private void m13639o() {
        C1222e c1222e = new C1222e(System.currentTimeMillis(), 1, 0);
        DeviceSource.bindSensorHub(c1222e);
//        C1218a.m11319a().m11337a(c1222e);
        m13640p();
        onSensorHubBinded();
    }

    class C1852r extends AsyncHttpResponseHandler {
        final /* synthetic */ BaseSCActivity f10364a;

        C1852r(BaseSCActivity baseSCActivity) {
            this.f10364a = baseSCActivity;
        }

        public void onFailure(int i, Header[] headerArr, byte[] bArr, Throwable th) {
        }

        public void onSuccess(int i, Header[] headerArr, byte[] bArr) {
            if (bArr != null) {
                String str = new String(bArr);
                GuliLog.tracePrintInfo("BaseSCActivity", "updateBindedSensorhub result:" + str);
                if (!WebResponse.getWebResponse(this.f10364a.getApplicationContext(), str).m11659c()) {
                }
            }
        }
    }

    private void m13640p() {
        C1327j.m11576a(getApplicationContext(), WebAPI.m11583a(getApplicationContext(), 2), new C1852r(this), true);
    }

    class C1853s extends Handler {
        final /* synthetic */ BaseSCActivity handlerActivity;

        C1853s(BaseSCActivity baseSCActivity) {
            this.handlerActivity = baseSCActivity;
        }

        public void handleMessage(Message message) {
            switch (message.what) {
                case 4098:
                    GuliLog.tracePrintInfo("BaseSCActivity", "MSG_LOAD_DATA_COMPLETE......................" + new Date().toString());
//                    EventBus.getDefault().post(new EventLoadDataComplete());
                    break;
                case 4099 /*4099*/:
//                    if (this.handlerMainFragmentActivity.f8827b.checkMiliFwUpgrade()) {
//                        new C3821i(new C1854t(this)).m9824f();
//                        return;
//                    }
//                    C3910h f = XMBLEManager.m25161f();
//                    if (f != null && f.m25440a()) {
//                        C3941i q = Utils.m8095q();
//                        if (q != null) {
//                            new C3936d().m25547a(q.f19893h, null);
//                        }
//                    }
                    break;
                case 4100:
//                    GuliLog.tracePrintInfo("BaseSCActivity", "shoes sync data complete");
//                    if (this.handlerMainFragmentActivity.f8827b.checkShoesFwUpgrade()) {
//                        this.handlerMainFragmentActivity.f8827b.upgrade(2);
//                    }
                    break;
                case 4101:
                    this.handlerActivity.m13636m();
                    if (DeviceSource.hasBindWeight() && this.handlerActivity.f8823L) {
                        LuaUtil.getInstance(this.handlerActivity.mContext).m15967b();
                        this.handlerActivity.m13660b(false);
                    }
                    if (DeviceSource.hasBindBracelet() || DeviceSource.hasBindShoesDevice()) {
                        EventBus.getDefault().post(new EventBtOnOff(0));
                    }
                    break;
                case 4102:
                    this.handlerActivity.m13638n();
                    if (DeviceSource.hasBindBracelet() || DeviceSource.hasBindShoesDevice()) {
                        EventBus.getDefault().post(new EventBtOnOff(1));
                    }
                    if (DeviceSource.hasBindWeight()) {
                        LuaUtil.getInstance(this.handlerActivity.mContext).addWeightConnectFailedDynamic();
                        this.handlerActivity.m13660b(false);
                    }
                    break;
                case 4103:
                    this.handlerActivity.m13660b(false);
                    break;
                case 4104:
                    this.handlerActivity.m13622f(message.arg1);
                    break;
                case 4105:
//                    if (this.handlerActivity.f8827b.checkWeightFwUpgrade()) {
//                        this.handlerActivity.f8827b.upgrade(1);
//                    } else {
                        this.handlerActivity.m13650z();
//                    }
                    break;
                case 4112:
                    GuliLog.m12370d("BaseSCActivity", "addWeightConnectFailedDynamic");
                    LuaUtil.getInstance(this.handlerActivity.mContext).addWeightConnectFailedDynamic();
                    this.handlerActivity.m13660b(false);
                    break;
                case 4113:
                    this.handlerActivity.onWeightScanTimeout();
                    break;
                case 4114:
                    this.handlerActivity.onWeightFound();
                    break;
                case 4115:
                    this.handlerActivity.m13657b();
                    break;
                case 4116:
                    this.handlerActivity.m13605a((HwConnStatus) message.obj);
                    break;
                default:
                    break;
            }
            return;
        }
    }

    class C1855u extends BroadcastReceiver {
        final /* synthetic */ BaseSCActivity thisActivity;
        Message message;

        C1855u(BaseSCActivity baseSCActivity) {
            this.thisActivity = baseSCActivity;
        }

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            GuliLog.tracePrintInfo("BaseSCActivity", action);
            if ("android.net.conn.CONNECTIVITY_CHANGE".equals(action)) {
                this.thisActivity.m13647w();
            } else if (action.equals(BLEService.mStrConStatusChanged)) {
                message = new Message();
                message.what = 4116;
                message.obj = intent.getParcelableExtra(BLEService.mStrExtraParam);
                GuliLog.m12370d("BaseSCActivity", "INTENT_ACTION_CONN_STATUS_CHANGED......!!!" + message.obj);
                this.thisActivity.mHandler.sendMessage(message);
            } else if (action.equals(BLEService.mStrSyncDataStatusChanged)) {
                HwSyncDataStatus hwSyncDataStatus = (HwSyncDataStatus) intent.getParcelableExtra(BLEService.mStrExtraParam);
                if (hwSyncDataStatus.m25417d()) {
                    if (hwSyncDataStatus.m25420g()) {
                        if (hwSyncDataStatus.m25421h().m25105d()) {
                            this.thisActivity.mHandler.sendEmptyMessage(4100);
                        } else {
                            this.thisActivity.mHandler.sendEmptyMessage(4099);
                        }
                        C0668a.m8128a(this.thisActivity.mContext, C0668a.ek, C0668a.dA);
                    } else {
                        C0668a.m8128a(this.thisActivity.mContext, C0668a.ek, C0668a.dB);
                    }
                    this.thisActivity.m13649y();
                }
                EventBus.getDefault().postSticky(hwSyncDataStatus);
                return;
            }
            if ("android.bluetooth.adapter.action.STATE_CHANGED".equals(action)) {
                switch (intent.getIntExtra("android.bluetooth.adapter.extra.STATE", Integer.MIN_VALUE)) {
                    case 10 /*10*/:
                        this.thisActivity.mHandler.sendEmptyMessage(4102);
                        return;
                    case 12 /*12*/:
                        this.thisActivity.mHandler.sendEmptyMessage(4101);
                        return;
                    default:
                        return;
                }
            }
            if (BLEService.mStrDynamicStatusChanged.equals(action)) {
                this.thisActivity.mHandler.sendEmptyMessage(4103);
            } else if (action.equals(BLEService.mStrBatStatusChanged)) {
                HwBatteryStatus hwBatteryStatus = (HwBatteryStatus) intent.getParcelableExtra(BLEService.mStrExtraParam);
//                if (hwBatteryStatus.f19644e == 1) {
//                    this.thisMainFragmentActivity.m13612c(hwBatteryStatus.f19645f);
//                } else if (hwBatteryStatus.f19644e == 2) {
//                    this.thisMainFragmentActivity.m13645u();
//                } else if (hwBatteryStatus.f19644e == 3) {
//                    this.thisMainFragmentActivity.m13644t();
//                }
                EventBus.getDefault().post(hwBatteryStatus);
                return;
            }
            int intExtra;
            if (BLEService.mStrWeightConStatusChanged.equals(action)) {
                intExtra = intent.getIntExtra(BLEService.mStrExtraParam, -1);
                message = new Message();
                message.what = 4104;
                message.arg1 = intExtra;
                this.thisActivity.mHandler.sendMessage(message);
                EventBus.getDefault().post(new C3917o(intExtra));
            } else if (action.equals("android.intent.action.CLOSE_SYSTEM_DIALOGS")) {
                action = intent.getStringExtra("reason");
                if (!TextUtils.isEmpty(action)) {
                    GuliLog.m12370d("BaseSCActivity", "ACTION_CLOSE_SYSTEM_DIALOGS:" + action);
                    if (action.equals("lock") || action.equals("homekey")) {
                        this.thisActivity.mHandler.sendEmptyMessage(4115);
                    }
                }
            } else if (action.equals(BLEService.mStrDevStatusChanged)) {
                intExtra = intent.getIntExtra(BLEService.mStrExtraParam, -1);
                // looks like it is for the ring
//                if (!Keeper.readPersonInfo().miliConfig.vibrate || !this.thisMainFragmentActivity.m13616d(intExtra)) {
//                }
            } else if (action.equals(BLEService.mStrWeightLowBat)) {
//                EventBus.getDefault().post(new EventWeightLowBattery());
            } else if (action.equals("android.intent.action.SCREEN_OFF")) {
                Keeper.setEnterAppTrace(3);
            }
        }
    }

    private void m13641q() {
        this.mHandler = new C1853s(this);
        this.mBroadcastReceiver = new C1855u(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        intentFilter.addAction(BLEService.mStrConStatusChanged);
        intentFilter.addAction(BLEService.mStrSyncDataStatusChanged);
        intentFilter.addAction(BLEService.mStrDynamicStatusChanged);
        intentFilter.addAction(BLEService.mStrBatStatusChanged);
        intentFilter.addAction(BLEService.mStrWeightConStatusChanged);
        intentFilter.addAction("android.bluetooth.adapter.action.STATE_CHANGED");
        intentFilter.addAction("android.intent.action.CLOSE_SYSTEM_DIALOGS");
        intentFilter.addAction(BLEService.mStrDevStatusChanged);
        intentFilter.addAction(BLEService.mStrWeightLowBat);
        intentFilter.addAction("android.intent.action.SCREEN_OFF");
        registerReceiver(this.mBroadcastReceiver, intentFilter);
    }

    private void m13642r() {
        m13625g(false);
    }

    class C1856v implements C1041c {
        final /* synthetic */ BaseSCActivity f10368a;

        C1856v(BaseSCActivity baseSCActivity) {
            this.f10368a = baseSCActivity;
        }

        public void m9840a (int i) { // m14942a
            LuaUtil.getInstance(this.f10368a.mContext).m15965b(i);
            this.f10368a.mHandler.sendEmptyMessage(4103);
        }
    }

    private void m13643s() {
        C3942j a = XMBLEManager.m25149a();
        if (a != null) {
            a.m25585a(new C1856v(this));
        }
    }

//    private void m13644t() {
//        aT e = new aT(this).m647a((int) R.drawable.app_icon).m664a(getString(R.string.bracelet_notify_title)).m673b(getString(R.string.bracelet_notify_text)).m687e(true);
//        e.m668a(new long[]{100, 500, 500, 500});
//        e.m654a(PendingIntent.getBroadcast(this, 0, new Intent(C4220a.bM), 268435456));
//        ((NotificationManager) getSystemService("notification")).notify(3, e.m676c());
//    }

    private void m13645u() {
        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);
    }

//    private void m13646v() {
//        Intent intent = new Intent();
//        intent.setClass(this, FwLowBatteryActivity.class);
//        startActivity(intent);
//    }

    private void m13647w() {
        Object obj = null;
        Object obj2 = 1;
        NetworkInfo[] allNetworkInfo = ((ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).getAllNetworkInfo();
        int i = 0;
        while (i < allNetworkInfo.length) {
            if (allNetworkInfo[i].getType() != 1) {
                int i2;
                if (allNetworkInfo[i].getType() != 9) {
                    if (allNetworkInfo[i].getType() == 0 && allNetworkInfo[i].isConnected()) {
                        obj2 = null;
                        i2 = 1;
                        break;
                    }
                } else if (allNetworkInfo[i].isConnected()) {
                    obj2 = null;
                    i2 = 1;
                    break;
                }
            } else if (allNetworkInfo[i].isConnected()) {
                obj = 1;
                break;
            }
            i++;
        }
        obj2 = null;
        if (obj != null) {
            if (obj2 != null) {
                m13648x();
            }
            m13598C();
        }
    }

    private void m13648x() {
        if (Keeper.readPersonInfoBaseNeedSync().getNeedSyncServer() != 0) {
            GuliLog.tracePrintInfo(TAG, "Sync person info when net connected");
            Utils.m7998a(this.mContext, Keeper.readPersonInfo());
        }
        Utils.m8099t(this.mContext);
        EventBus.getDefault().post(new EventWifiConnected());
    }

    private void m13649y() {
        Calendar instance = Calendar.getInstance();
        if (Keeper.readSyncTime(DataManager.m12179a().m12239r()) > -1) {
            instance.setTimeInMillis(Keeper.readSyncTime(DataManager.m12179a().m12239r()));
        }
        Calendar instance2 = Calendar.getInstance();
        if (instance.get(Calendar.DAY_OF_YEAR) != instance2.get(Calendar.DAY_OF_YEAR)) {
            C0668a.m8127a(this.mContext, C0668a.el, instance2.get(Calendar.HOUR_OF_DAY));
        }
    }

    // start to sync the weight data
    private void m13650z() {
        C0668a.m8126a(this.mContext, C0708b.aw);
        XMBLEManager.m25153a(new SyncWeightDataCB(this.mContext), Keeper.readPersonInfo().uid);
    }

    protected void m13651a() {
        GuliLog.m12370d(TAG, "=================onEnterInForeground=================");
        this.f8831y = false;
        this.f8822K = false;
        this.f8820I = System.currentTimeMillis();
    }

    protected void onDeviceBonded(int i) { // m13652a
    }

    public void m13593a(FilterBleDevInfo filterBleDevInfo) {
    }

    public void onScanedDevice(C3907e c3907e, FilterBleDevInfo filterBleDevInfo) {
        if (this.mHandler != null && Keeper.readWeightHwInfo().address.equalsIgnoreCase(c3907e.btDev.getAddress())) {
            this.mHandler.sendEmptyMessage(f8807q);
            if (!this.f8823L) {
                GuliLog.m12370d(TAG, "connectWeightDevice:" + c3907e);
                // connect to weight BLE service
                XMBLEManager.m25150a(c3907e.btDev, false);
                this.f8821J = System.currentTimeMillis();
                if (!this.mHandler.hasMessages(addWeightConnectFailedDynamic)) {
                    this.mHandler.sendEmptyMessageDelayed(addWeightConnectFailedDynamic, 60000);
                }
            }
            if (c3907e.scaleWeightInfo != null) {
                //TBD DEBUG
                Log.e(TAG, GuliLog.stackTracePrint() + c3907e.scaleWeightInfo.toString());
                EventBus.getDefault().post(c3907e.scaleWeightInfo);
            }
        }
    }

    public boolean m13655a(Context context) {
        List runningTasks = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1);
        if (!(runningTasks == null || runningTasks.isEmpty())) {
            ComponentName componentName = ((ActivityManager.RunningTaskInfo) runningTasks.get(0)).topActivity;
            GuliLog.m12370d(TAG, "topActivity:" + componentName.flattenToString());
            if (!componentName.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }

    boolean m13656a(boolean z) {
        return m13608a(z, false);
    }

    protected void m13657b() {
        GuliLog.m12370d(TAG, "=================onEnterInBackground=================");
        if (this.f8831y) {
            GuliLog.m12370d(TAG, "has onEnterInBackground,now return!!!");
            return;
        }
        this.f8831y = true;
        this.f8812A = false;
        this.f8823L = false;
        m13638n();
        if (0 == this.f8820I || this.f8821J <= this.f8820I) {
            GuliLog.tracePrintInfo(TAG, "connect device fail...");
//            C0668a.m8128a(this.mContext, C0668a.fO, WeiboAuthException.DEFAULT_AUTH_ERROR_CODE);
        } else {
            GuliLog.tracePrintInfo(TAG, "connect device time " + this.f8821J + " mStartTime " + this.f8820I);
            C0668a.m8128a(this.mContext, C0668a.fO, C3657a.f18328f + (this.f8821J - this.f8820I));
        }
        this.f8820I = 0;
//        if (this.f8827b != null) {
//            this.f8827b.enableFwUpgrade();
//        }
    }

    protected void m13658b(int i) {
    }

    public void m13595b(FilterBleDevInfo filterBleDevInfo) {
    }

    protected void m13660b(boolean z) {
    }

    protected void m13661c() {
    }

    public boolean m13662c(boolean z) {
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (defaultAdapter == null || defaultAdapter.isEnabled()) {
            return true;
        }
        if (!z && this.f8812A) {
            return false;
        }
        if (XMBLEManager.m25167l()) {
            return true;
        }
        startActivityForResult(new Intent("android.bluetooth.adapter.action.REQUEST_ENABLE"), f8793c);
        this.f8815D = true;
        return false;
    }

    protected void onSensorHub() { // m13663d
    }

    protected void onSensorHubBinded() { // m13664e
    }

    protected void onWeightScanTimeout() { // m13665f
        GuliLog.tracePrintInfo(TAG, "onWeightScanTimeout");
        EventBus.getDefault().post(new EventScaleStatus(PersonInfo.INCOMING_CALL_DISABLE_BIT));
    }

    protected void onWeightFound() { // m13666g
        GuliLog.tracePrintInfo(TAG, "onWeightFound");
        EventBus.getDefault().post(new EventScaleStatus(EventScaleStatus.SCALE_FOUNDED));
        this.mHandler.removeMessages(f8806p);
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        GuliLog.tracePrintInfo(TAG, "onActivityResult:" + i);
        if (i == f8793c) {
            switch (i2) {
                case -1 /*-1*/:
                    this.f8812A = false;
                    break;
                case 0 /*0*/:
                    C3247b.m22930a((Context) this, getString(R.string.failed_enable_bt), 1).show();
                    this.f8812A = true;
                    EventBus.getDefault().post(new EventBtOnOff(1));
                    break;
                default:
            }
        } else if (i == f8794d) {
            this.f8813B = false;
        } else if (i == f8795e) {
            this.f8813B = false;
        }
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        GuliLog.m12370d(TAG, "onCreate...");
        this.mContext = getApplicationContext();
//        this.f8827b = FwUpgradeManager.getDefault(this.mContext);
//        this.f8827b.enableFwUpgrade();
        this.f8832z = true;
        this.f8814C = (DeviceSource.hasBindBracelet() | DeviceSource.hasBindShoesDevice()) | DeviceSource.hasBindSensorHub();
        m13609b((Context) this);
        m13641q();
        //TBD this is for ring
//        m13600a(0, 0);
        EventBus.getDefault().register(this);
//        C1647J.m13015c(this.mContext);
//        C1145z.m10581c(this.mContext);
//        C1364i.m11813b(this.mContext);
    }

    protected void onDestroy() {
        unregisterReceiver(this.mBroadcastReceiver);
        GuliLog.m12370d(TAG, "onDestory..........................................");
        this.mHandler.removeCallbacksAndMessages(null);
        this.mHandler = null;
        this.f8812A = false;
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    //获取测量结果
    public void onEventBackgroundThread(ScaleWeightInfo c3925e) {
        GuliLog.tracePrintInfo(TAG, "onEvent WeightAdvData:" + c3925e);
        GuliLog.tracePrintInfo("BOOL", "onBaseSCActivity MemberInfoSetWeightActivity.isUserWeighting " + false + " BabyWeightActivity.isAdultWithBabyWeighting " + false + " BabyWeightActivity.isAdultWeighting " + false);
        //if (MemberInfoSetWeightActivity.f11435h || BabyWeightActivity.f11070b || BabyWeightActivity.f11069a) {
        if (MemberInfoSetWeightActivity.f11435h) {
            GuliLog.tracePrintInfo(TAG, "MemberInfoSetWeightActivity is running and here do nothing");
        } else if (c3925e != null) {
            m13606a(c3925e);
        }
    }

    public void onEventMainThread(EventDeviceBoundState eventDeviceBoundState) {
        int i;
        GuliLog.m12370d(TAG, "EventDeviceBoundState:" + eventDeviceBoundState);
        int i2 = eventDeviceBoundState.mType;
        boolean z = eventDeviceBoundState.mState == 1;
        switch (i2) {
            case C4220a.f20886i /*0*/:
                LuaEvent.getInstance(this.mContext).deleteLuaOfStepAssociatedItem();
                if (z) {
                    EventBus.getDefault().postSticky(new HwConnStatus(XMBLEManager.m25158c() ? 6 : 4));
                }
                this.f8818G = z;
                i = 0;
                break;
            case 1 /*1*/:
                i = 1;
                break;
            case 3 /*3*/:
                i = 2;
                if (!DeviceSource.hasBindBracelet()) {
                    LuaEvent.getInstance(this.mContext).deleteLuaOfStepAssociatedItem();
                }
                this.f8819H = z;
                break;
            default:
                i = 0;
                break;
        }
//        this.f8827b.setUpgradeState(i, true);
        if (z) {
            onDeviceBonded(i2);
        } else {
            m13658b(i2);
        }
    }

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    protected void onPause() {
        this.f8832z = false;
        super.onPause();
    }

    protected void onResume() {
        super.onResume();
        XMBLEManager.startBleServiceForDevice(this.mContext, Keeper.readBraceletBtInfo().f19636b, Utils.m8093p());
        m13626h();
        if (this.f8831y || this.f8832z) {
            m13651a();
        }
    }

    protected void onStop() {
        super.onStop();
        this.f8832z = false;
        if (m13655a((Context) this)) {
            this.mHandler.sendEmptyMessage(f8808r);
        }
//        if (this.f8826a != null) {
//            this.f8826a.m13249a();
//            this.f8826a = null;
//        }
    }
}
